import config from '@/config/config.js';
class SocketChat {
	constructor(uid,_uni) {
		this.uid = uid
		this.url = `${config.wsBaseUrl}chat/${uid}`
		this.innerAudioContext = _uni.innerAudioContext
		this.socketTask = uni.connectSocket({
		    url: this.url, 
		    complete: (res)=> {
				console.log(res)
			}
		});
		return this
	}
	
	getSocketTask(){
		return this.socketTask
	}
	sockOnMessage(){
		this.socketTask.onMessage((data)=>{
			// 接收服务端消息data
			console.log(data)
		})
	}
	sockSend(param,_uni){
		console.log("发送数据：" + JSON.stringify(param))
		if(_uni){
			this._uni = _uni
		}
		// 发送数据
		this.socketTask.send({
			// 消息数据
			data:JSON.stringify(param),
			// 成功回调
			success:res=>{
				console.log(res)
			},
			// 失败回调
			fail:err=>{
				console.log(err)
			}
		})
	}
	sockClose(){
		// 连接关闭
		this.socketTask.close({
			// 关闭连接，1000表示正常关闭
			code:1000,
			// 成功回调
			success:res=>{
				console.log(res)
			},
			// 失败回调
			fail:err=>{
				console.log(err)
			}
		})
	}
	sockOnOpen(){
		// 监听连接打开
		this.socketTask.onOpen(res=>{
			// res服务端返回数据
			console.log(this.url + " - 连接打开")
			clearInterval(this.timeer)
		})
	}
	set_uni(_uni){
		this._uni = _uni
	}
	initSocketTask(_uni){
		// 接收服务端消息data
		this._uni = _uni
		console.log("初始化socket连接")
		this.sockOnOpen()
		// 接收消息监听
		this.socketTask.onMessage((data)=>{
			let res = JSON.parse(data.data)
			if(!(this._uni.$store.user.uid == res.uid)){
				this._uni.$store.innerAudioContext.play()
				uni.showTabBarRedDot({
					index:2
				})
				let msgIndex = this._uni.$store.chatList.findIndex(item=>item.uid == res.uid)
				if(msgIndex == -1){
					let chatObj = {
						toFriendId: res.toFriendId,
						friendName: res.friendName,
						avatar: res.avatar,
						uid: res.uid,
						messageList:[res]
					}
					msgIndex = 0
					this._uni.$store.chatList.unshift(chatObj)
				}else{
					this._uni.$store.chatList[msgIndex].messageList.push(res)
				}
				if(this._uni.messageArray){
					let message = {
						uid:res.uid,
						toFriendId:res.toFriendId,
						contentType:5,
					}
					this.sockSend(message)
					// res.messageStatus = 1
					this._uni.scrollToBottom()
				}
				
			}else{
				if(this._uni.messageArray){
					let msgIndex = this._uni.$store.chatList.findIndex(item=>item.uid == this._uni.friendUid)
					this._uni.$store.chatList[msgIndex].messageList.push(res)
					this._uni.messages.messageList = this._uni.messageArray()
					// this._uni.$nextTick(e =>{
					// 	this._uni.scrollIntoId = 'msg_' + this._uni.messages.messageList[this._uni.messages.messageList.length - 1].id
					// 	// this._uni.scrollIntoId = 'msg_bottom'
					// })
					this._uni.scrollToBottom()
				}
			}
			
			this._uni.$store.chatList.forEach(item=>{
				item.tip = 0
				item.messageList.forEach(chatMsg=>{
					if(chatMsg.messageStatus == 2 && chatMsg.uid != this._uni.$store.user.uid){
						item.tip += 1
					}
				})
			})
			this._uni.$utils.setStorage("chatList",this._uni.$store.chatList)
			console.log(res)
			console.log(this._uni.$store.chatList)
		})
		// 连接关闭回调
		this.socketTask.onClose(res=>{
			// 连接关闭回调
			console.log(this.url + " - 连接关闭")
		})
		// 连接发生错误
		this.socketTask.onError(err=>{
			console.log(this.url + " - 连接错误")
			console.log(err)
			this.timeer = setInterval(()=>{
				console.log("重试连接")
				let sock = new SocketChat(this.uid)
				this.socketTask = sock
				this.initSocketTask()
			},1000)
		})
	}
}

export {
	SocketChat
}